package com.autotest2;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2023/2/10 13:43
 */
public class autoTest2 {
    private ChromeDriver driver = new ChromeDriver();
    public void start2(){
        driver.get("https://www.baidu.com");
       // driver.get("https://tool.lu/");
       // driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
    }

    public void test2(){
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        String title = driver.getTitle();
        System.out.println(title);
        String url = driver.getCurrentUrl();
        System.out.println(url);
    }
    public void window() throws InterruptedException {
        //每个页面都有一个句柄（唯一标识）
        //获取当前页面的句柄
        String window = driver.getWindowHandle();
        //System.out.println(window);
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        Thread.sleep(3000);
        String window2 = driver.getWindowHandle();
       // System.out.println(window2);
        //获取所有标签页的句柄
       Set<String> allwindow =  driver.getWindowHandles();
       for (String nowWindow:allwindow){
           if(nowWindow != window){
               //跳转
               driver.switchTo().window(nowWindow);
           }
       }
//        String title = driver.getTitle();
//        System.out.println(title);
//        String url = driver.getCurrentUrl();
//        System.out.println(url);
        /**
         * 执行脚本，最底部，最顶部的自动化
         */
       driver.executeScript("window.scroll(0,document.body.scrollHeight)");
       Thread.sleep(3000);
        driver.executeScript("window.scroll(0,document.body.scrollTop)");
        Thread.sleep(3000);
    }

    /**
     * 前进，后退
     * @throws InterruptedException
     */
    public void navigate() throws InterruptedException {
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#nav > div > div > ul > li.active > a")).click();
        Thread.sleep(1000);
        driver.navigate().back();
        Thread.sleep(3000);
        driver.navigate().forward();
        Thread.sleep(3000);
        /**
         * 弹窗的确认/取消
         */
//        Alert alert = driver.switchTo().alert();
//        alert.accept()/dismiss();

    }

    /**
     * 根据索引来选择
     */
    public void index(){
        WebElement element = driver.findElement(By.cssSelector(""));
        Select select = new Select(element);
        select.selectByIndex(0);
        //通过value来选择
        select.selectByValue("11");
        //通过可见文本来选择
        select.selectByVisibleText("");
    }

    /**
     * 文件上传
     */
    public void transportFile(){
        WebElement element = driver.findElement(By.cssSelector(""));
        element.sendKeys("");

    }

    /**
     * 自动化截图
     * @throws IOException
     */
    public void jietu() throws IOException {
        File src = driver.getScreenshotAs(OutputType.FILE);
        //指定路劲
        File file = new File("./src/test/PNG/jietu.png");
        FileUtils.copyFile(src,file);

    }

    public void end2(){
        driver.quit();
    }
}
